package co.fitstart.admin.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.annotation.Dynamic;
import com.demo2do.core.support.Page;

import co.fitstart.admin.Constants;
import co.fitstart.admin.dao.VisitorRecordDao;
import co.fitstart.admin.entity.content.VisitorRecordSearch;
import co.fitstart.admin.handler.UserHandler;
import co.fitstart.admin.service.VisitorRecordService;
import co.fitstart.entity.site.Site;
import co.fitstart.entity.user.TelephoneResource;
import co.fitstart.entity.user.User;
import co.fitstart.entity.user.VisitorRecord;
import co.fitstart.entity.user.enums.VisitChannel;

/**
 * 
 * @author dan
 *
 */
@Service("visitorRecordService")
public class VisitorRecordServiceImpl extends DataServiceImpl<VisitorRecord> implements VisitorRecordService {
    
    @Dynamic
    private VisitorRecordDao visitorRecordDao;
    
    @Autowired
    private UserHandler userHandler;

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.VisitorRecordService#list(co.fitstart.entity.site.Site, java.util.Date, com.demo2do.core.support.Page)
     */
    public List<VisitorRecord> list(Site site, Date date, Page page) {
        List<VisitorRecord> result = new ArrayList<VisitorRecord>();
        if(site == null) {
            result = visitorRecordDao.list(date, page.getBeginIndex(), page.getMaxResultRecords());
        } else {
            result = visitorRecordDao.list(site, date, page.getBeginIndex(), page.getMaxResultRecords());
        }
        
        page.calculate(result.size());
        return result.size() > page.getEveryPage() ? result.subList(0, page.getEveryPage()) : result;
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.VisitorRecordService#list(java.util.List, java.util.Date, com.demo2do.core.support.Page)
     */
    public List<VisitorRecord> list(List<Site> sites, Date date, Page page) {
        List<VisitorRecord> result = visitorRecordDao.list(sites, date, page.getBeginIndex(), page.getMaxResultRecords());
        
        page.calculate(result.size());
        return result.size() > page.getEveryPage() ? result.subList(0, page.getEveryPage()) : result;
    }

    /* (non-Javadoc)
     * @see co.fitstart.admin.service.VisitorRecordService#save(co.fitstart.entity.user.VisitorRecord)
     */
    public void save(VisitorRecord visitorRecord) {

        // 1. save record to database
        genericDaoSupport.save(visitorRecord);
        
        // 2. create user and userAccount
        String mobile = visitorRecord.getMobile();
        String userName = visitorRecord.getName();
        User user = userHandler.create(mobile, userName, true, Constants.USER_ACCOUNT_MOBILE_KEY, mobile);
        
        if(visitorRecord.getVisitChannel() == VisitChannel.F) {
            TelephoneResource telephoneResource = new TelephoneResource(user, visitorRecord.getSite(), visitorRecord.getReceiver(), visitorRecord.getOccurDate());
            
            genericDaoSupport.save(telephoneResource);
        }
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.VisitorRecordService#search(co.fitstart.admin.entity.content.VisitorRecordSearch)
     */
    @SuppressWarnings("unchecked")
    public List<VisitorRecord> search(VisitorRecordSearch search) {
        String s = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        return genericDaoSupport.searchForList(s, parameters);
    }
}
